using System;
using System.Collections.Generic;
using NHibernate;

namespace FwkLight.Domain.Query
{
  public abstract class HqlQuery<T, TOutput> : IDomainQuery<T, TOutput>
  {
    protected IQuery _query;

    protected IList<T> Load ( )
    {
      return _query.List<T>();
    }

		protected IList<T> Load(ISession session)
		{
			return Load(session, false);
		}

		protected TOutput LoadUnique(ISession session)
		{
			Initialise(session, false);
			return _query.UniqueResult<TOutput>();
		}

		protected IList<T> Load(ISession session, bool includingInactive)
		{
			Initialise(session, includingInactive);
			return Load();
		}

    public IDomainQuery<T, TOutput> Initialise ( ISession session )
    {
      return Initialise(session, false);
    }

    public IDomainQuery<T, TOutput> Initialise ( ISession session, bool includingInactive )
    {
      _query = session.CreateQuery(
        (includingInactive ? HQLForAllItems : HQLForActiveItems)
        + Sorting);

      SetParameters(_query);

      return this;
    }

    public virtual void SetParameters ( IQuery query ) { }

		public virtual TOutput GetResult()
		{
			throw new NotImplementedException();
		}

  	protected abstract string HQLForActiveItems { get; }

    protected abstract string HQLForAllItems { get; }

    protected abstract string Sorting { get; }
  }
}